package com.linksfield.topup.utils;

import java.text.SimpleDateFormat;
import java.util.*;

public class DateFillUtils {

    public static List<Map> fillDate(String timeCategory,String startD,String endD){
        //如果是年则返回12个月,天按小时处理
        List<Map> dList = new ArrayList<Map>();
        if (!timeCategory.equals("Y") && !timeCategory.equals("D")){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = null;
            Date endDate = null;
            try {
                startDate = sdf.parse(startD);
                endDate = sdf.parse(endD);
            }catch (Exception e){
                e.printStackTrace();
            }
            //两个日期相隔days天
            long days = (endDate.getTime() - startDate.getTime()) / (1000*3600*24);
            Map<String,String> dMap = new HashMap<String, String>();
            dMap.put("x",startD);
            dMap.put("y","0");
            dList.add(dMap);
            Calendar calendar = Calendar.getInstance();
            Date tempDate = startDate;
            for (int i=0;i<days;i++){
                calendar.setTime(tempDate);
                calendar.add(Calendar.DAY_OF_WEEK,1);
                String nextDay = sdf.format(calendar.getTime());
                dMap = new HashMap<String, String>();
                dMap.put("x",nextDay);
                dMap.put("y","0");
                dList.add(dMap);
                tempDate = calendar.getTime();
            }
            return dList;
        }else if (timeCategory.equals("D")){
            //按天统计时候，需要返回24小时
            Map<String,String> dMap = null;
            for (int i=0;i<=23;i++){
                dMap = new HashMap<String, String>();
                if (i <= 9){
                    dMap.put("x","0"+i);
                    dMap.put("y","0");
                    dList.add(dMap);
                }else {
                    dMap.put("x",""+i);
                    dMap.put("y","0");
                    dList.add(dMap);
                }
            }
            return dList;
        }else if (timeCategory.equals("Y")){
            //按年统计返回12个月份,第一个月就是去年的这一个月
            Map dMap = null;
            int s1 = Integer.parseInt(startD.substring(5,7));
            String y1 = startD.substring(0,5);
            for (int i =s1;i<=12;i++){
                dMap = new HashMap();
                if (i<=9){
                    dMap.put("x",y1+"0"+i);
                    dMap.put("y",0);
                    dList.add(dMap);
                }else {
                    dMap.put("x",y1+i);
                    dMap.put("y",0);
                    dList.add(dMap);
                }
            }
            int s2 = Integer.parseInt(endD.substring(5,7));
            String y2 = endD.substring(0,5);
            for (int i= 1;i<=s2;i++){
                dMap = new HashMap();
                if (i<=9){
                    dMap.put("x",y2+"0"+i);
                    dMap.put("y",0);
                    dList.add(dMap);
                }else {
                    dMap.put("x",y2+i);
                    dMap.put("y",0);
                    dList.add(dMap);
                }
            }
        }
        return dList;
    }

    public static void main(String [] args){
        String x = "2019-03-28";
        System.out.println(x.substring(5,7));
        System.out.println(Integer.parseInt(x.substring(5,7)));
    }
}
